package com.szm.userserver.webui;

import com.szm.userserver.entity.UserMongo;
import com.szm.userserver.service.UserMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserMongoController {

    @Autowired
    private UserMongoService userMongoService;

    @RequestMapping("/get/{id}")
    public UserMongo getUserById(@PathVariable int id) {
        return userMongoService.getUserById(id);
    }

    @RequestMapping("/getall")
    public List<UserMongo> findAll() {
        return userMongoService.findAll();
    }

    @RequestMapping("/adduser")
    public String addUser() {
        UserMongo userMongo = new UserMongo(16, "szm" + 16, 16);
        userMongoService.insert(userMongo);
        return "add success";
    }

    @RequestMapping("/edituser/{id}")
    public String editUser(@PathVariable int id) {
        UserMongo userMongo = new UserMongo(id, "szm" + 001, 1);
        userMongoService.update(userMongo);
        return "edit success";
    }

    @RequestMapping("/deluser/{id}")
    public String delUser(@PathVariable int id) {
        userMongoService.remove(id);
        return "delete success";
    }

    @RequestMapping("/adduserlist")
    public String addUserList(){
        List<UserMongo> list=new ArrayList<>();
        for (int i=5;i<=15;i++){
            list.add(new UserMongo(i,"szm"+i,i));
        }
        userMongoService.insertAll(list);
        return "addlist success";
    }

//    public List<UserMongo> findByPage(@PathVariable int start,UserMongo userMongo){
//        PageRequest pageable=new PageRequest(start, 2);
//        return userMongoService.findByPage(userMongo,pageable);
//    }
}
